package com.surfacing.rcp.db.util;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.time.StopWatch;

import org.apache.log4j.Logger;

import com.surfacing.rcp.db.dao.*;
import com.surfacing.rcp.db.dao.factory.DaoFactory;
import com.surfacing.rcp.model.entity.*;

public class DbCacheHelper {

	private static final Logger log = Logger.getLogger(DbCacheHelper.class);

	private static final DienststelleDAO dsDAO = DaoFactory.MYSQL
			.createDienststelleDAO();
	private static final EinsatzartDAO eaDAO = DaoFactory.MYSQL
			.createEinsatzartDAO();
	private static final TransportzielDAO tpzDAO = DaoFactory.MYSQL
			.createTransportzielDAO();
	private static final AnforderungDAO anforderungDAO = DaoFactory.MYSQL
			.createAnforderungDAO();
	private static final NfsDAO nfsDAO = DaoFactory.MYSQL.createNfsDAO();
	private static final DoctorDAO doctorDAO = DaoFactory.MYSQL
			.createDoctorDAO();
	
	private static final StatDienststelleDAO dsStatDAO = DaoFactory.MYSQL
			.createStatDienststelleDAO();
	private static final StatEinsatzartDAO eaStatDAO = DaoFactory.MYSQL
			.createStatEinsatzartDAO();
	private static final StatTransportzielDAO tpzStatDAO = DaoFactory.MYSQL
			.createStatTransportzielDAO();
	private static final StatAnforderungDAO anforderungStatDAO = DaoFactory.MYSQL
			.createStatAnforderungDAO();
	private static final StatNfsDAO nfsStatDAO = DaoFactory.MYSQL.createStatNfsDAO();
	private static final StatDoctorDAO doctorStatDAO = DaoFactory.MYSQL
			.createStatDoctorDAO();
	
	private static final JournalDAO journalDAO = DaoFactory.MYSQL
			.createJournalDAO();

	private static final Map<Object, Dienststelle> mapDienststelle = new HashMap<Object, Dienststelle>();
	private static final Map<Object, Einsatzart> mapEinsatzart = new HashMap<Object, Einsatzart>();
	private static final Map<Object, Transportziel> mapTransportziel = new HashMap<Object, Transportziel>();
	private static final Map<Object, Anforderung> mapAnforderung = new HashMap<Object, Anforderung>();
	private static final Map<Object, Nfs> mapNfs = new HashMap<Object, Nfs>();
	private static final Map<Object, Doctor> mapDoctor = new HashMap<Object, Doctor>();
	
	private static final Map<Object, StatDienststelle> mapStatDienststelle = new HashMap<Object, StatDienststelle>();
	private static final Map<Object, StatEinsatzart> mapStatEinsatzart = new HashMap<Object, StatEinsatzart>();
	private static final Map<Object, StatTransportziel> mapStatTransportziel = new HashMap<Object, StatTransportziel>();
	private static final Map<Object, StatAnforderung> mapStatAnforderung = new HashMap<Object, StatAnforderung>();
	private static final Map<Object, StatNfs> mapStatNfs = new HashMap<Object, StatNfs>();
	private static final Map<Object, StatDoctor> mapStatDoctor = new HashMap<Object, StatDoctor>();
	
	private static final Map<Object, Journal> mapJournal = new HashMap<Object, Journal>();

	public static final void init() {
		StopWatch sw = new StopWatch();
		sw.reset();
		sw.start();
		log.info("DbCache initialized");
		try {
			for (Journal j : journalDAO.getAllJournal()) {
				mapJournal.put(j.getId(), j);
			}
			for (Dienststelle ds : dsDAO.getAllDienststelle()) {
				mapDienststelle.put(ds.getId(), ds);
			}
			for (Einsatzart ea : eaDAO.getAllEinsatzart()) {
				mapEinsatzart.put(ea.getId(), ea);
			}
			for (Transportziel tpz : tpzDAO.getAllTransportziel()) {
				mapTransportziel.put(tpz.getId(), tpz);
			}
			for (Anforderung anf : anforderungDAO.getAllAnforderung()) {
				mapAnforderung.put(anf.getId(), anf);
			}
			for (Nfs n : nfsDAO.getAllNfs()) {
				mapNfs.put(n.getId(), n);
			}
			for (Doctor d : doctorDAO.getAllDoctor()) {
				mapDoctor.put(d.getId(), d);
			}
			
			for (StatDienststelle ds : dsStatDAO.getAllStatistics()) {
				mapStatDienststelle.put(ds.getName(), ds);
			}
			for (StatEinsatzart ea : eaStatDAO.getAllStatistics()) {
				mapStatEinsatzart.put(ea.getName(), ea);
			}
			for (StatTransportziel tpz : tpzStatDAO.getAllStatistics()) {
				mapStatTransportziel.put(tpz.getName(), tpz);
			}
			for (StatAnforderung anf : anforderungStatDAO.getAllStatistics()) {
				mapStatAnforderung.put(anf.getName(), anf);
			}
			for (StatNfs n : nfsStatDAO.getAllStatistics()) {
				mapStatNfs.put(n.getName(), n);
			}
			for (StatDoctor d : doctorStatDAO.getAllStatistics()) {
				mapStatDoctor.put(d.getName(), d);
			}
			sw.stop();
			log.info("DbCache successfully initialized - " + sw.getTime());
		} catch (SQLException e) {
			log.error("Error on initializing Db Data.", e);
		}
	}

	public static Map<Object, Dienststelle> getMapdienststelle() {
		return mapDienststelle;
	}

	public static Map<Object, Einsatzart> getMapeinsatzart() {
		return mapEinsatzart;
	}

	public static Map<Object, Transportziel> getMaptransportziel() {
		return mapTransportziel;
	}

	public static Map<Object, Anforderung> getMapanforderung() {
		return mapAnforderung;
	}

	public static Map<Object, Nfs> getMapnfs() {
		return mapNfs;
	}

	public static Map<Object, Doctor> getMapdoctor() {
		return mapDoctor;
	}

	public static Map<Object, Journal> getMapjournal() {
		return mapJournal;
	}

}
